import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const BlankPage = () => import('@/views/BlankPage.vue')
// 认证相关页面
const Login = () => import('@/views/Login.vue')
const Register = () => import('@/views/Register.vue')
const CounterRegister = () => import('@/views/CounterRegister.vue')
const CounterRefund = () => import('@/views/CounterRefund.vue')
const CounterCharge = () => import('@/views/CounterCharge.vue')
const FeeRefund = () => import('@/views/FeeRefund.vue')
const FeeLog = () => import('@/views/FeeLog.vue')
const PatientView = () => import('@/views/PatientView.vue')
const CheckRequest = () => import('@/views/CheckRequest.vue')
const CheckPatient = () => import('@/views/CheckPatient.vue')
const CheckInput = () => import('@/views/CheckInput.vue')
const CheckResult = () => import('@/views/CheckResult.vue')
const DoctorDiagnosis = () => import('@/views/DoctorHome.vue')
const DoctorRecord = () => import('@/views/DoctorRecord.vue')
const DepartmentManagement = () => import('@/views/DepartmentManagement.vue')
const PharmacyDispense = () => import('@/views/pharmacy/PharmacyDispense.vue')
const PharmacyRefund = () => import('@/views/pharmacy/PharmacyRefund.vue')
const PharmacyStorage = () => import('@/views/pharmacy/PharmacyStorage.vue')
const PharmacyRecord = () => import('@/views/pharmacy/PharmacyRecord.vue')
const LabRequest = () => import('@/views/lab/labRequest.vue')
const LabPatient = () => import('@/views/lab/labPatient.vue')
const LabInput = () => import('@/views/lab/labInput.vue')
const LabResult = () => import('@/views/lab/labResult.vue')
const DisposelRequest = () => import('@/views/disposel/disposeRequest.vue')
const DisposelPatient = () => import('@/views/disposel/disposePatient.vue')
const DisposelInput = () => import('@/views/disposel/disposeInput.vue')
const DisposelResult = () => import('@/views/disposel/disposeResult.vue')
// 路由配置
// 将登录/注册前缀统一为 /auth/*，避免与“挂号 Register”语义混淆
const routes = [
  { path: '/', name: 'Home', component: BlankPage, meta: { title: '首页' } },
  { path: '/auth/login', name: 'Login', component: Login, meta: { title: '登录', public: true } },
  { path: '/auth/register', name: 'Register', component: Register, meta: { title: '注册', public: true } },
  // 挂号收费
  { path: '/register/counter', name: 'CounterRegister', component: CounterRegister, meta: { title: '窗口挂号' } },
  { path: '/register/refund', name: 'CounterRefund', component: CounterRefund, meta: { title: '窗口退号' } },
  { path: '/fee/charge', name: 'FeeCharge', component: CounterCharge, meta: { title: '收费' } },
  { path: '/fee/refund', name: 'FeeRefund', component: FeeRefund, meta: { title: '退费' } },
  { path: '/fee/log', name: 'FeeLog', component: FeeLog, meta: { title: '患者费用信息' } },
  // 医生工作
  { path: '/doctor/patient', name: 'DoctorPatient', component: PatientView, meta: { title: '患者查看' } },
  { path: '/doctor/diagnosis', name: 'DoctorDiagnosis', component: DoctorDiagnosis, meta: { title: '医生诊疗' } },
  { path: '/doctor/record', name: 'DoctorRecord', component: DoctorRecord, meta: { title: '看诊记录' } },
  // 检查管理
  { path: '/check/request', name: 'CheckRequest', component: CheckRequest, meta: { title: '检查申请管理' } },
  { path: '/check/patient', name: 'CheckPatient', component: CheckPatient, meta: { title: '患者录入(检查)' } },
  { path: '/check/input', name: 'CheckInput', component: CheckInput, meta: { title: '检查录入' } },
  { path: '/check/manage', name: 'CheckManage', component: CheckResult, meta: { title: '结果管理' } },
  // 检验管理
  { path: '/inspect/apply', name: 'InspectApply', component: LabRequest, meta: { title: '检验申请' } },
  { path: '/inspect/patient', name: 'InspectPatient', component: LabPatient, meta: { title: '患者录入(检验)' } },
  { path: '/inspect/input', name: 'InspectInput', component: LabInput, meta: { title: '检验录入' } },
  { path: '/inspect/manage', name: 'InspectManage', component: LabResult, meta: { title: '结果管理' } },
  // 药房管理
  { path: '/pharmacy/dispense', name: 'PharmacyDispense', component: PharmacyDispense, meta: { title: '药房发药' } },
  { path: '/pharmacy/return', name: 'PharmacyReturn', component: PharmacyRefund, meta: { title: '药房退药' } },
  { path: '/pharmacy/storage', name: 'PharmacyStorage', component: PharmacyStorage, meta: { title: '药品管理' } },
  { path: '/pharmacy/records', name: 'PharmacyRecords', component: PharmacyRecord, meta: { title: '交易记录' } },
  // 处置管理
  { path: '/disposal/apply', name: 'DisposalApply', component: DisposelRequest, meta: { title: '处置申请' } },
  { path: '/disposal/patient', name: 'DisposalPatient', component: DisposelPatient, meta: { title: '患者录入(处置)' } },
  { path: '/disposal/input', name: 'DisposalInput', component: DisposelInput, meta: { title: '处置录入' } },
  { path: '/disposal/manage', name: 'DisposalManage', component: DisposelResult, meta: { title: '结果管理' } },
  // 系统管理
  { path: '/system/department', name: 'DepartmentManagement', component: DepartmentManagement, meta: { title: '科室管理' } },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

// 全局前置守卫：未登录强制跳转登录页
router.beforeEach((to, from, next) => {
  const user = useUserStore()
  const isPublic = to.meta?.public || to.path.startsWith('/auth')
  if (!isPublic && !user.token) {
    return next({ path: '/auth/login', query: { redirect: to.fullPath } })
  }
  next()
})

export default router
